﻿using LD.Host;
using System;
using System.Collections.Concurrent;

namespace LD
{
    public class LogHelper
    {
        private static LogHelper instance;
        private static object _lock = new object();
        public delegate void LogDelegate(string content, LogLevel type);
        public event LogDelegate LogEvent;
        private ConcurrentDictionary<string, string> AlarmDic = new ConcurrentDictionary<string, string>();
        private ConcurrentDictionary<string, string> WarrDic = new ConcurrentDictionary<string, string>();
        private static object mErrLock = new object();
        private static object mWarrLock = new object();

        public static LogHelper Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (_lock)
                    {
                        if (instance == null)
                            instance = new LogHelper();
                    }

                }
                return instance;
            }
        }
        public void Info(string content)
        {
            Log4.Info(content);
            LogEvent?.Invoke(content, LogLevel.一般);
        }
        public void Alarm(string code, string content, bool isReset = false)
        {
            lock (mErrLock)
            {
                if (isReset)

                if (!AlarmDic.ContainsKey(content))
                {
                    AlarmDic.TryAdd(content, content);

                    Log4.Alarm(content);


                    LogEvent?.Invoke($"【{code}】{content}", LogLevel.严重);

                    //DBHelper.Instance.Sql.Insertable(new TB_Alarm()
                    //{
                    //    Code = code,
                    //    Content = content,
                    //    CreateTime = DateTime.Now,
                    //}).ExecuteCommand();
                }
            }
        }
        public void ParamChange(string content)
        {
            Log4.ParamChange(content);
        }
        public void CCD(string content)
        {
            Log4.CCD(content);
        }
        public void Auto(string content)
        {
            Log4.Auto(content);
        }
        public void Hive(string content)
        {
            Log4.Hive(content);
        }
        public void Bali(string content)
        {
            Log4.Bali(content);
        }
        public void Warr(string content, LogLevel logLevel = LogLevel.警告)
        {
            lock (mWarrLock)
            {
                if (logLevel == LogLevel.警告报警)
                {
                    if (!WarrDic.ContainsKey(content))
                    {
                        WarrDic.TryAdd(content, content);
                        LogEvent?.Invoke($"{content}", logLevel);
                        Log4.Warr(content);
                    }
                }
                else
                {
                    LogEvent?.Invoke($"{content}", logLevel);
                    Log4.Warr(content);
                }
            }
        }
        public void Clear()
        {
            AlarmDic.Clear();
            WarrDic?.Clear();
        }
        public bool IsAlarmIng
        {
            get
            {
                return AlarmDic.Count > 0;
            }
        }
        public bool IsWarrIng
        {
            get
            {
                return WarrDic.Count > 0;
            }
        }
    }
}
